package com.gary.controller;

import com.gary.entity.Employee;
import com.gary.mapper.IEmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @CacheConfig(cacheNames = "emp")
 *  统一此类中所有的cacheNames
 * @author pengrui
 * @date 2020/11/27 - 9:58
 */
//@CacheConfig(cacheNames = "emp")
@RestController
public class EmployeeController {

    @Autowired
    private IEmployeeMapper iEmployeeMapper;

    /**
     *  @Cacheable 第一次查到数据后放入缓存中，第二次直接从缓存中获取
     *      cacheNames/value: 指定缓存的名字
     *      key: 缓存数据使用的key；默认使用方法参数的值 即 id的值
     *      keyGenerator: key的生成器，可以自己指定key的生成器的组件id
     *          key/keyGenerator二选一
     *      cacheManager: 指定缓存管理器；或者cacheResolver指定获取解析器
     *      condition：指定符合条件的情况下使用缓存
     *          el: condition=“#{id}>10”
     *      unless: 除非条件
     *      sync 是否使用异步模式
     *
     *      org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "emp")
    @GetMapping("/queryOne/{id}")
    public Employee queryById(@PathVariable("id") Integer id) {
        Employee employee = iEmployeeMapper.queryById(id);
        return employee;
    }

    /**
     *  @CachePut(cacheNames = "user") 更新缓存操作 每次调用此方法 是 都会更新一次到缓存中
     *  测试步骤
     *      1、查询1 号员工
     *      2、更新1号员工
     *      3、在查询1号员工
     *      cacheNames = "user", key = "#result.id"
     * @param employee
     * @return
     */
    @CachePut(cacheNames = "emp")
    @GetMapping("/updateEmp")
    public Employee updateEmp(Employee employee) {
        iEmployeeMapper.updateEmp(employee);
        return employee;
    }

    /**
     * @CacheEvict 清楚缓存中的数据
     * CacheEvict
     *      beforeInvocation 默认 false 是在方法执行完成之后 在清楚缓存
     *      allEntries 默认为 false 清除cacheName的所有缓存数据
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "emp", beforeInvocation = true )
    @GetMapping("delEmp/{id}")
    public String delEmp(@PathVariable("id") Integer id) {
        Integer row = iEmployeeMapper.delEmp(id);
        System.out.println("是否删除成功："+row);
        //此处检 CacheEvict 是在方法执行前执行清楚缓存 还是在方法执行后执行缓存
        //throw new NumberFormatException("丢");
        return "success";
    }

    /**
     *  Caching 联合注解
     * @param lastName
     * @return
     */
    @Caching(
            cacheable = @Cacheable(cacheNames = "emp", key = "#lastName"),
            put = {
                    @CachePut(cacheNames = "emp", key = "#result.id"),
                    @CachePut(cacheNames = "emp", key = "#result.email")
            }
    )
    @GetMapping("/queryLastName/{lastName}")
    public Employee queryByLastName(@PathVariable("lastName") String lastName) {
        Employee employee = iEmployeeMapper.queryByLastName(lastName);
        System.out.println(employee);
        return employee;
    }
}
